{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy\n",
    "from numpy import real, imag, nan\n",
    "from scipy.io import loadmat\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "color_A = plt.cm.viridis(.3)\n",
    "color_B = plt.cm.viridis(.85)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.rcParams.update({'font.size': 20})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "filepath = '../tracking-output/simulated_PRN-10_PLL-BW-20.mat'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = loadmat(filepath)\n",
    "\n",
    "# Extract data\n",
    "time = data['time'][0]\n",
    "prn = data['prn'][0][0]\n",
    "acq_correlation = data['acq_correlation']\n",
    "acq_doppler_bins = data['acq_doppler_bins'][0]\n",
    "\n",
    "n0 = data['n0'][0][0]\n",
    "fs_IF = data['fs_IF'][0][0]\n",
    "f_center = data['f_center_IF'][0][0]\n",
    "B_DLL = data['B_DLL'][0][0]\n",
    "B_PLL = data['B_PLL'][0][0]\n",
    "\n",
    "prompt = data['prompt'][0]\n",
    "code_phase_truth = data['code_phase_truth'][0]\n",
    "carrier_phase_truth = data['carrier_phase_truth'][0]\n",
    "doppler_freq_truth = data['doppler_freq_truth'][0]\n",
    "code_phase = data['code_phase'][0]\n",
    "measured_code_phase = data['measured_code_phase'][0]\n",
    "filtered_code_phase = data['filtered_code_phase'][0]\n",
    "carrier_phase = data['carrier_phase'][0]\n",
    "measured_carrier_phase = data['measured_carrier_phase'][0]\n",
    "filtered_carrier_phase = data['filtered_carrier_phase'][0]\n",
    "doppler_freq = data['doppler_freq'][0]\n",
    "measured_doppler_freq = data['measured_doppler_freq'][0]\n",
    "filtered_doppler_freq = data['filtered_doppler_freq'][0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Set parameters for GPS L1CA signal\n",
    "f_carrier = 1.57542e9           # L1 carrier frequency (Hz)\n",
    "f_code = 1.023e6                # L1 C/A code rate (chips/s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig = plt.figure(figsize=(10, 7), dpi=300)\n",
    "ax = fig.add_subplot(111)\n",
    "k = 50\n",
    "i0, i1 = max([0, n0 - k]), min([acq_correlation.shape[1], n0 + k])\n",
    "truncated_corr = acq_correlation[:, i0:i1]\n",
    "truncated_corr /= numpy.std(truncated_corr)\n",
    "extent = (i0 * f_code / fs_IF, i1 * f_code / fs_IF, acq_doppler_bins[-1], acq_doppler_bins[0])\n",
    "im = ax.imshow(truncated_corr, aspect=.002, extent=extent, vmin=0, vmax=25)\n",
    "ax.text(.8, .9, 'PRN {0:02}'.format(prn), color='w', transform=ax.transAxes)\n",
    "cb = plt.colorbar(im)\n",
    "cb.set_label('Correlation Magnitude')\n",
    "\n",
    "ax.set_ylabel('Doppler [Hz]')\n",
    "ax.set_xlabel('Code Phase [chips]')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig = plt.figure(figsize=(10, 4), dpi=300)\n",
    "ax = fig.add_subplot(111)\n",
    "args = {'s': 4, 'alpha': .7}\n",
    "ax.scatter(time, real(prompt), color=color_A, **args)\n",
    "ax.scatter(time, imag(prompt), color=color_B, **args)\n",
    "ax.plot((time[0], time[-1]), (0, 0), color='k')\n",
    "ax.set_xlim(time[0], time[-1])\n",
    "ax.grid()\n",
    "ax.set_xlabel('Time [s]')\n",
    "ax.set_ylabel('I/Q Corr. Value')\n",
    "ax.legend([plt.Rectangle((nan, nan), 1, 1, color=c) for c in [color_A, color_B]], ['I', 'Q'], loc=4)\n",
    "ax.text(.02, .27, 'PRN {0:02}\\n{1:02} Hz PLL Bandwidth'.format(prn, B_PLL), transform=ax.transAxes)\n",
    "plt.tight_layout()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig = plt.figure(figsize=(10, 4), dpi=300)\n",
    "ax = fig.add_subplot(111)\n",
    "args = {'s': 4, 'alpha': 1}\n",
    "ax.scatter(time, measured_code_phase - code_phase_truth, color=color_A, **args)\n",
    "ax.scatter(time, filtered_code_phase - code_phase_truth, color=color_B, **args)\n",
    "ax.set_xlim(time[0], time[-1])\n",
    "ax.grid()\n",
    "ax.set_xlabel('Time [s]')\n",
    "ax.set_ylabel('Code Phase Error [chips]')\n",
    "ax.legend([plt.Rectangle((nan, nan), 1, 1, color=c) for c in [color_A, color_B]], ['Unfiltered', 'Filtered'], loc=4)\n",
    "ax.text(.02, .07, 'PRN {0:02}\\n{1:02} Hz PLL Bandwidth'.format(prn, B_PLL), transform=ax.transAxes)\n",
    "plt.tight_layout()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig = plt.figure(figsize=(10, 4), dpi=300)\n",
    "ax = fig.add_subplot(111)\n",
    "args = {'s': 4, 'alpha': 1}\n",
    "ax.scatter(time, measured_carrier_phase - carrier_phase_truth, color=color_A, **args)\n",
    "ax.scatter(time, filtered_carrier_phase - carrier_phase_truth, color=color_B, **args)\n",
    "ax.set_xlim(time[0], time[-1])\n",
    "# ax.set_ylim(-10, 1)\n",
    "ax.grid()\n",
    "ax.set_xlabel('Time [s]')\n",
    "ax.set_ylabel('Carrier Phase Error [cycles]')\n",
    "ax.legend([plt.Rectangle((nan, nan), 1, 1, color=c) for c in [color_A, color_B]], ['Unfiltered', 'Filtered'], loc=4)\n",
    "ax.text(.02, .07, 'PRN {0:02}\\n{1:02} Hz PLL Bandwidth'.format(prn, B_PLL), transform=ax.transAxes)\n",
    "plt.tight_layout()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig = plt.figure(figsize=(10, 4), dpi=300)\n",
    "ax = fig.add_subplot(111)\n",
    "args = {'s': 4, 'alpha': .5}\n",
    "ax.scatter(time, measured_doppler_freq - doppler_freq_truth, color=color_A, **args)\n",
    "ax.scatter(time, filtered_doppler_freq - doppler_freq_truth, color=color_B, **args)\n",
    "ax.set_xlim(time[0], time[-1])\n",
    "# ax.set_ylim(-1, 1)\n",
    "ax.grid()\n",
    "ax.set_xlabel('Time [s]')\n",
    "ax.set_ylabel('Doppler Error [Hz]')\n",
    "ax.legend([plt.Rectangle((nan, nan), 1, 1, color=c) for c in [color_A, color_B]], ['Unfiltered', 'Filtered'], loc=4)\n",
    "ax.text(.02, .07, 'PRN {0:02}\\n{1:02} Hz PLL Bandwidth'.format(prn, B_PLL), transform=ax.transAxes)\n",
    "plt.tight_layout()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig = plt.figure(figsize=(10, 4), dpi=300)\n",
    "ax = fig.add_subplot(111)\n",
    "args = {'s': 4, 'alpha': .5}\n",
    "ax.scatter(time, measured_doppler_freq, color=color_A, **args)\n",
    "ax.scatter(time, filtered_doppler_freq, color=color_B, **args)\n",
    "ax.scatter(time, doppler_freq_truth, color='k', **args)\n",
    "ax.set_xlim(time[0], time[-1])\n",
    "# ax.set_ylim(-1, 1)\n",
    "ax.grid()\n",
    "ax.set_xlabel('Time [s]')\n",
    "ax.set_ylabel('Doppler Error [Hz]')\n",
    "ax.legend([plt.Rectangle((nan, nan), 1, 1, color=c) for c in ['k', color_A, color_B]], ['Truth', 'Unfiltered', 'Filtered'], loc=4)\n",
    "ax.text(.02, .07, 'PRN {0:02}\\n{1:02} Hz PLL Bandwidth'.format(prn, B_PLL), transform=ax.transAxes)\n",
    "plt.tight_layout()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
